<?php
class OaService
{
	private $aid;
	private $oid;
	private $appPreM;//测试App
	private $appOnlineM;//正式App
	private $appM;//当前appModel
	private $appConfM;
	private $error;//1001订单不存在、1002设置当前app失败环境变量传错
	
	public $appconfig = array(
		'login_service'=>'service_login', 
		'ibw_service'=>'service_ibw', 
		'igw_service'=>'service_igw', 
		'pay_service'=>'service_pay',
		'billing_service'=>'service_billing', 
		'points_service'=>'service_points', 
		'prevent_obsession_service'=>'service_prevent_obsession',
		'help_service'=>'service_help', 
		'bonus_service'=>'service_bonus'
	);
	public $servers = array(
        'service_login',
        'service_ibw',
        'service_igw',
        'service_pay',
        'service_billing',
        'service_help',
        'service_bonus',
        'service_points',
        'service_prevent_obsession',
    );
	/*
	 * $aid 应用ID
	 */
	public function __construct($aid)
	{
		$this->aid = $aid;
		$this->appPreM = new OaAppModel( OaAppModel::APP_CONFIG_ENV_TEST );
		$this->appOnlineM = new OaAppModel( OaAppModel::APP_CONFIG_ENV_PRODUCTION );
		$this->appConfM = new OaAppConfigOrderModel();
	
		$rt = $this->appConfM->getAppLastConfigOrder($this->aid);
		if (empty($rt))
		{
			$this->error = 1001;
		}
		else
		{
			$this->oid = $rt['id'];				
		}
	}
	//根据环境设置当前app
	private function setAppM( $env )
	{
		if ( $env == OaAppModel::APP_CONFIG_ENV_TEST )
		{
			$this->appM = &$this->appPreM;
		}
		else if ($env == OaAppModel::APP_CONFIG_ENV_PRODUCTION) 
		{
			$this->appM = &$this->appOnlineM;;
		}
		else
		{
			$this->error = 1002;
		}
	}
	
	/**
	 * 得到task操作类型 开通、修改、关闭
	 * $sName 任务名称
	 */
	
	private function getOpType( $sName )
	{
		$temp = explode('_', $sName);
		return $temp[0];
	}
	private function getServiceName( $sName )
	{
		$temp = explode('_', $sName);
		return $temp[1] . '_' . $temp[2];		
	}
	 /**
     * 服务开通
     * @param string $servicename 服务名称，8大服务之一
     * @param OaAppModel &$appModel
     * @param OaAppConfigOrderModel &$appConfigOrderModel
     * @param int $appId
     * @param int $orderId
     * @param int $env
     * @param array $params  服务配置参数
     * @return int|boolean 成功，失败
     */
    public function openService($serviceName, &$appModel, &$appConfigOrderModel, $appId, $orderId, $env, $params = array())
    {
    	/*
    	参数检查;
    	
    	获取服务当前信息GetService;
    	
    	if(服务已开通)
    	{
    		return UpdateService();
    	}
    	
    	if(empty($params))
    	{
    		$params = 当前配置;
    	}

   		调用服务网关;
		$appModel修改app service status(开通成功/开通失败);
    	*/
    }
    
    
	 /**
     * 服务关闭
     * @param string $servicename 服务名称，8大服务之一
     * @param OaAppModel &$appModel
     * @param OaAppConfigOrderModel &$appConfigOrderModel
     * @param int $appId
     * @param int $orderId
     * @param int $env
     * @return int|boolean 成功，失败
     */
    public function closeService($serviceName, &$appModel, &$appConfigOrderModel, $appId, $orderId, $env)
    {
    	/*
    	参数检查;
    	
    	获取服务当前信息GetService;
    	
    	if(服务未开通)
    	{
    		return;
    	}
    	
   		调用服务网关;
		$appModel修改app service status(关闭成功/关闭失败);
    	*/
    }
    

	 /**
     * 服务更新
     * @param string $servicename 服务名称，8大服务之一
     * @param OaAppModel &$appModel
     * @param OaAppConfigOrderModel &$appConfigOrderModel
     * @param int $appId
     * @param int $orderId
     * @param int $env
     * @param array $params  服务配置参数
     * @return int|boolean 成功，失败
     */
    public function updateService($serviceName, &$appModel, &$appConfigOrderModel, $appId, $orderId, $env, $params = array())
    {
    	/*
    	参数检查;
    	
    	获取服务当前信息GetService;
    	
    	if(服务未开通)
    	{
    		return OpenService();
    	}
    	
    	if(empty($params))
    	{
    		$params = 当前配置;
    	}

   		调用服务网关;
		$appModel修改app service status(修改成功/修改失败);
    	*/
    }
    
    
	 /**
     * 获取应用全部服务信息
     * @param int $env
     * @return array 服务信息
     */
    public function getServiceList( $env )
    {
    	$this->setAppM( $env );
    	
    	$apply = $this->appM->getApp( $this->aid );
		$tasklist = $this->appConfM->getConfigOrderTasks( $this->oid, $env);
		
		$faketasklist = array();
    	foreach ( $this->appconfig as $k =>$v )
    	{
    		$status = $apply[$v.'_status'];
    		if($this->task_exist($tasklist,'open_'.$k) || $this->task_exist($tasklist,'modify_'.$k))
    			$status = OaAppModel::APP_SERVICE_STATUS_SELECTED;
    		else if($this->task_exist($tasklist,'close_'.$k))
    			$status == OaAppModel::APP_SERVICE_STATUS_CLOSED;
    		$faketasklist[] = array(
    			'job_name' => 'open_'.$k,
    			'status' => $status,
    			'data' => $apply[$v],
    		);
    	}
    	
    	return $faketasklist;
    }
    
    
	 /**
     * 获取服务信息列表
     * @param string $servicename 服务名称，8大服务之一
     * @param OaAppModel &$appModel
     * @param int $appId
     * @param int $env
     * @return array ('name'=>'login_service',status=>1,....)
     */
    public function getService( $sName, $env )
    {
    	$this->setAppM( $env );
    	
    	$apply = $this->appM->getApp( $this->aid );
    	$service = $this->getServiceName( $sName );
    	$res = $apply[ $this->appconfig[$service] ] ;
    	$res['status'] = $apply[ $this->appconfig[$service].'_status' ] ;
    	$res['name'] = $service ;
    	return $res;
    }
    
    
	 /**
     * 更新服务信息
     * @param string $sName 服务名称 8大服务之一
     * @param int $env
     * @param int $status (开通/关闭/更新)
     * @param array $params  服务配置参数
     * @param bool $realtime  实时生效
     * @return array 服务信息
     */
    public function setService( $sName, $env, $params = array(), $realtime = false )
    {
    	$this->setAppM( $env );
    	$optype = $this->getOpType( $sName );
    	$service = $this->getServiceName( $sName );
    	
    	$apply = $this->appM->getApp( $this->aid );
    	$serviceOpened = ( $apply[ $this->appconfig[$service].'_status' ] == OaAppModel::APP_SERVICE_STATUS_OPENED ) ? 1 : 0; //1为开通0为未开通
    	
/*        $taskFlag = $this->appConfM()->getConfigOrderTaskByJobName( $this->oid, $sName );
        if ( empty( $taskFlag ) )
        {
        	$this->appConfM()->createConfigTask($this->oid, $sName, $params);
        }
*/        
        $tasklist = $this->appConfM->getConfigOrderTasks( $this->oid, $env);
        foreach($tasklist as $atask)
        {
        	if($this->getServiceName($atask['job_name']) == $service && $atask['status'] == OaAppConfigOrderModel::CONFIG_TASK_STATUS_VALID)
        		$this->appConfM->modifyConfigTask($atask['id'],array('status' => OaAppConfigOrderModel::CONFIG_TASK_STATUS_INVALID));
        }
         
    	if($realtime)
    	{

    	}
    	else
    	{
/*	    	switch( $optype.'_'.$serverFlag )
	    	{
	    		case 'open_1':
	    		case 'open_2':
	    		case 'modify_1':
	    		case 'modify_2':
	    		case 'close_1':
	    			$this->appConfM()->modifyConfigTask($localtask[0]['id'], array( 'job_name'=>$sName, 'data'=>serialize($params) ));
					break;
	    		case 'close_2':
					break;					
	    	}
*/
	    	switch( $optype.'_'.$serviceOpened )
	    	{
	    		case 'open_1':
	    			$sName = 'modify_' . $service;break;
	    		case 'modify_0':
	    			$sName = 'open_' . $service;break;
	    		case 'close_1':
	    			$params = array();break;
	    		case 'close_0':
					return true;					
	    	}
	    	if(!empty($params))
	    		$this->appM->modifyApp($this->aid, array( $this->appconfig[$service] => $params));
   			$this->appConfM->createConfigTask($this->oid, $sName, $this->transferServiceParam( $service, $params), $env);

    	}
    	
    	return true;
    }
    
    public function transferServiceParam( $sName, $params = array())
    {
    	return $params;
    }
    
    public function task_exist($exist_service,$service_name)
	{
		foreach($exist_service as $v)
		{
			if($v['job_name'] == $service_name && $v['status'] == OaAppConfigOrderModel::CONFIG_TASK_STATUS_VALID)
				return true;
		}
		return false;
			
	}

}
?>